home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-11-03 | 10.1 KB | 327 lines | [TEXT/CWIE] |
- PROGRAM ProcDoggie;
-
- {-------------------------------------------------------------------------------
- File: ProcDoggie.p
-
- Contains: Main program file for the ProcDoggie application.
-
- Written by: Forrest Tanaka
-
- Copyright: © 1988-1997 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "DSC Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
- --------------------------------------------------------------------------------
- #
- # ProcDoggie.p is the root file for ProcDoggie. It contains the main entry
- # point and the PROGRAM statement, but relies on the other source files
- # included with this application to actually implement the functionality.
- #
- # This file itself contains the following functionality:
- # 1. startup code
- # 2. AppleEvent handlers
- # 3. main event loop
- #
- # UEvent.p is the unit responsible for actually fielding events.
- #
- -------------------------------------------------------------------------------}
- {[j=20/57/1$] Pasmat Options}
-
-
- (*******************************************************************************
- * Used Units
- *******************************************************************************)
-
- USES
- AppleEvents
- ,Fonts
- ,ToolUtils
- ,DiskInit
- ,SegLoad
- ,GestaltEqu
-
- (* Application *)
- ,UGlobals
- ,UEmergMem
- ,UProcessUtils
- ,UMenuHandler
- ,UProcessGuts
- ,UEvents
- ;
-
-
- (*******************************************************************************
- * Constants
- *******************************************************************************)
-
- CONST
- kBecomingActive = TRUE; {Pass to DoActivateEvt; indicates becoming active}
-
-
- (*******************************************************************************
- * Global Variables
- *******************************************************************************)
-
- VAR
- gProcessListWind: WindowPtr; {Pointer to the process list window}
-
-
- {$S Main}
- (*******************************************************************************
- * DoneRequiredParams - Done processing required params; OK?
- *
- * DoneRequiredParams checks to see if the AppleEvent specified by the
- * anAppleEvent parameter has any required parameters that we haven’t yet
- * processed. If there aren’t any left, then noErr is returned. If there are
- * required parameters that haven’t been processed yet, then errAEEventNotHandled
- * is returned. If any other errors occur, then that error code is returned.
- *******************************************************************************)
-
- FUNCTION DoneRequiredParams (anAppleEvent: AppleEvent): OSErr;
-
- VAR
- typeCode: DescType; {Type of AppleEvent attribute found; ignored}
- actualSize: Size; {Actual size of parameters; ignored}
- error: OSErr;
-
- BEGIN
- (* Are there any required parameters in AppleEvent we didn’t process? *)
- error := AEGetAttributePtr (anAppleEvent, keyMissedKeywordAttr,
- typeWildCard, (*<*)typeCode, NIL, 0, (*<*)actualSize);
- IF error = errAEDescNotFound THEN
- (* No required parameters left, so no error *)
- DoneRequiredParams := noErr
- ELSE IF error = noErr THEN
- (* There was at least one required parameter we didn’t process *)
- DoneRequiredParams := errAEEventNotHandled
- ELSE
- (* Some other error happened *)
- DoneRequiredParams := error
- END;
-
-
- {$S Main}
- (*******************************************************************************
- * HandleAEquit - Handler for 'quit' AppleEvent
- *
- * This is the AppleEvent handler for the 'quit' AppleEvent as passed in the
- * quitAppleEvent parameter by the AppleEvent Manager. The DoQuit routine is
- * called which causes this application to quit at the start of the next
- * iteration of the main event loop.
- *
- * Though the quit AppleEvent doesn’t contain any parameters, the standard thing
- * to do in reaction to any AppleEvent is to check to see if there are any
- * required parameters in the AppleEvent that this routine doesn’t recognise.
- * DoneRequiredParms checks for this condition and returns an error if there are
- * in fact required parameters in the AppleEvent or if some other error occurs
- * during the check.
- *******************************************************************************)
-
- FUNCTION HandleAEquit (VAR quitAppleEvent: AppleEvent;
- VAR reply: AppleEvent;
- handlerRefCon: LongInt): OSErr;
-
- VAR
- error: OSErr;
-
- PROCEDURE RecoverError (errorCode: OSErr);
-
- BEGIN
- HandleAEquit := errorCode;
- EXIT (HandleAEquit)
- END;
-
- BEGIN
- {$unused reply}
- {$unused handlerRefCon}
- (* quit AE has no parms, but check in case the client requires any *)
- error := DoneRequiredParams (quitAppleEvent);
- IF error <> noErr THEN
- RecoverError (error);
-
- (* Handle the Quit command *)
- error := DoQuit;
- IF error <> noErr THEN
- RecoverError(error);
-
- HandleAEquit := noErr
- END;
-
-
- {$S Startup}
- (*******************************************************************************
- * StartUp - Do whatever has to be done to initialize the application
- *
- * This routine is called after the heap is initialized to initialize the
- * application. This involves initializing the toolbox, emergency memory, and
- * loading up the menus. If any errors occur while doing this, StartUp displays
- * an alert telling the user what the error was and then ExitToShell is called.
- * This is an unusual way to react to errors, and I only do it here because it’s
- * so early in execution that there really isn’t much else that can be done.
- *
- * See this UEmergMem unit in this application for details about emergency
- * memory.
- *******************************************************************************)
-
- PROCEDURE StartUp;
-
- CONST
- kSysHandler = TRUE; {Specifies that AE handler is in system heap}
-
- VAR
- error: OSErr;
-
- PROCEDURE HandleError (messageClass: Integer;
- messageIndex: Integer);
-
- VAR
- junkError : OSErr;
- junkItemHit: Integer; {Result of alert; ignored}
-
- BEGIN
- junkError := ShowStopAlert (messageClass, messageIndex, junkItemHit);
- ExitToShell
- END;
-
- VAR
- gestaltResponse: LongInt;
-
- BEGIN
- (* Initialize the toolbox *)
- InitGraf (@qd.thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs (NIL);
-
- (* Check environment. *)
- IF (Gestalt(gestaltSystemVersion, gestaltResponse) <> noErr) | ( gestaltResponse < $0700) THEN
- HandleError(rMiscErrMessages, kMiscSystemTooSmall);
-
- (* We make the assumption that if we have System 7 then we have a bunch of other
- System 7 features that we rely on, namely:
-
- o new Standard File calls
- o AppleEvents
- o Process Manager
-
- While this is counter to the philosophy of Gestalt, it sure makes the code smaller.
- It's also a sensible approach for "big bang" system releases like System 7.
- Besides, ProcDoggie can do nothing useful without the Process Manager (which
- was introduced with System 7), so there's no point in us 'limping along',
- using old style Standard File calls and so on, when we can't display a
- process list.
- *)
-
- (* Initialise some boring parts of our application. Basically this involves
- making some UPPs.
- *)
- InitGlobals;
- InitProcessGuts;
-
- (* Initialize emergency memory *)
- InitEmergMem;
- IF FailLowMemory (0) THEN
- HandleError (rMemErrMessages, kMemErrAppOpenMsg);
-
- (* Load the menus and draw the menu bar *)
- StartMenus;
- IF FailLowMemory (0) THEN
- HandleError (rMemErrMessages, kMemErrAppOpenMsg)
- ELSE IF gError <> noErr THEN
- IF gError = memFullErr THEN
- HandleError (rMemErrMessages, kMemErrAppOpenMsg)
- ELSE IF gError = resNotFound THEN
- HandleError (rResErrMessages, kResErrAppDamageMsg)
- ELSE
- HandleError (rMiscErrMessages, kMiscErrUnknownMsg);
-
- (* Install the AppleEvent handler *)
- error := AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(@HandleAEquit), 0, NOT kSysHandler);
- IF (error = memFullErr) | FailLowMemory (0) THEN
- HandleError (rMemErrMessages, kMemErrAppOpenMsg)
- ELSE IF error <> noErr THEN
- HandleError (rMiscErrMessages, kMiscErrUnknownMsg)
- END;
-
-
- {$S Main}
- (*******************************************************************************
- * EventLoop - Main event loop for this application
- *
- * This is the main event loop of this application. During every iteration of
- * the event loop, the menus are kept up-to-date, and the Process List window and
- * all of the open Process Information windows are given time to update
- * themselves to current conditions. Also, NoEmergMem is called to detect
- * whether the emergency memory was used. If it was, then RecoverEmergMem is
- * called in an attept to get it back. If it can’t, then some commands could be
- * disabled until the memory can be recovered.
- *******************************************************************************)
-
- PROCEDURE EventLoop;
-
- VAR
- anEvent: EventRecord; {An incoming event}
-
- BEGIN
- FixMenus;
- InitCursor;
- gQuitting := FALSE;
-
- (* We loop “forever,” or until the Quit handler calls ExitToShell *)
- WHILE NOT gQuitting DO
- BEGIN
- (* Give all open windows some time *)
- IdleAllProcessWindows;
-
- (* Try to reallocate emergency memory if it’s been used *)
- IF NoEmergMem THEN
- RecoverEmergMem;
-
- (* Fix the menus to reflect current conditions *)
- FixMenus;
-
- (* It’s time to get and examine an event *)
- IF WaitNextEvent (everyEvent, (*<*)anEvent, kMaxSleepTime, NIL) THEN
- BEGIN
- DoEvent(anEvent);
- END
- END
- END;
-
-
- BEGIN
- (* Set up the heap *)
- MaxApplZone;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
-
- (* Do anything that must be done at program start-up *)
- StartUp;
- {$ifc not GENERATINGCFM}
- UnloadSeg (@StartUp);
- {$endc}
-
- (* Set the default launch mode *)
- SetLaunchMode (kJustLaunch);
-
- (* Open the process list window *)
- gProcessListWind := CreateProcessListWindow;
-
- (* Enter the main event loop *)
- EventLoop
- END.
-